<?php

namespace app\adminapi\lists\organize;

use adminmatrix\admin\lists\BaseListsData;
use app\adminapi\model\Admin\Admin;
use app\adminapi\model\organize\AdminDepartment;
use app\adminapi\model\organize\AdminPosition;
class PositionLists extends BaseListsData
{
    protected AdminPosition $model;
    protected function initialize()
    {
        $this->model = new AdminPosition;
    }

    public function lists(): array|object
    {
        // TODO: Implement lists() method.
        return buildTree($this->model
            ->withSearch($this->withSearch , $this->params)
            ->append(['department_id_name' , 'creator_id_name' , 'leader_id_name'])

            ->withAttr('department_id_name' ,function ($query , $data){
                return AdminDepartment::where('id' , $data['department_id'])->value('title');
            })
            ->withAttr('creator_id_name' ,  function($query , $data){
                return Admin::where('id' , $data['creator_id'])->value('nickname');
            }) -> withAttr('leader_id_name' , function ($query , $data){
                return  Admin::where('id' , $data['leader_id'])->value('nickname');
            })
            ->select()->toArray()
        );
    }

    public function count(): int
    {
        // TODO: Implement count() method.
        return $this->model->withSearch($this->withSearch , $this->params)->count();
    }
}